﻿using System.Collections.Generic;
using System.Linq;
using Inscriptionem.Models.Base;

namespace Inscriptionem.Models.Blog.Views.Controls
{
    public class PostNavidationViewModel : BaseRequestModel
    {
        protected readonly ORM.DB DB;

        public PostNavidationViewModel(int ID)
        {
            DB = new ORM.DB();

            this.ID = ID;
        }

        public int ID { get; set; }

        public ORM.Post Previous { get { return DB.Posts.OrderByDescending(p => p.ID).FirstOrDefault(p => p.ID < ID && p.PostCategories.Select(pc => pc.Category).Any(c => c.Section.Name == Section && c.Section.Language.Name == Language)); } }
        public ORM.Post Next { get { return DB.Posts.FirstOrDefault(p => p.ID > ID && p.PostCategories.Select(pc => pc.Category).Any(c => c.Section.Name == Section && c.Section.Language.Name == Language)); } }

        public IEnumerable<ORM.Post> Set
        {
            get
            {
                var @set = DB.Posts.First(p => p.ID == ID).Set;

                if (@set == null)
                {
                    return new List<ORM.Post>();
                }

                return DB.Posts.Where(p => p.Set.ID == @set.ID);
            }
        }
    }
}